﻿package rd.hardcore.ui.pad 
{
	import aze.motion.eaze;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.ui.Keyboard;
	import fr.digitas.flowearth.conf.Conf;
	import rd.debug.Debug;
	import rd.hardcore.datas.Track;
	import rd.hardcore.engine.datas.DatasManager;
	import rd.hardcore.engine.sound.HardcoreGhettoSoundSystem;
	import rd.hardcore.events.ResizeEvent;
	import rd.hardcore.events.SaveManagerEvent;
	import rd.hardcore.events.SoundSystemEvent;
	import rd.hardcore.params.GlobalEvents;
	import rd.hardcore.params.GlobalParams;
	import rd.hardcore.ui.controls.Controls;
	import rd.hardcore.ui.pad.Pad;
	
	/**
	 * Pads Manager
	 * @author Romain Prache
	 */
	public class PadsManager extends Sprite
	{
		
		/* VAR */
		public var ID			: int;
				
		protected var vecPads	: Vector.<Pad>;
		protected var container	: Sprite;
		
		
		/* Position Pads Manager */
		public function PadsManager( _id : int ) 
		{			
			ID = _id;
			
			container = new Sprite();
			container.x = container.y = 10;
			addChild(container);
			
			vecPads 	= new Vector.<Pad>();
		}
		
		
		/* Building Padz */
		public function build(pads : XML) : void
		{
			// Get Pads
			var node	: XML;
			for each( node in pads.children() )
			{
				var pad	: Pad = new Pad();
				pad.build(node);
				vecPads.push(pad);
			}
			
			displayGrid();
		}
		
		
		protected function displayGrid(e:Event = null):void
		{
			var i:int = 0;
			var possible_n	: int = 10;
			
			// Association & Display
			for each( var pad:Pad in vecPads )
			{				
				// display
				pad.x = int( (i % possible_n)		* ( Conf.getNumber("sizePad") + 10)  	);
				pad.y = int( int(i / possible_n)	* ( Conf.getNumber("sizePad") + 10) 	);
				
				if (int(i / possible_n) == 2) pad.x += 20 + (GlobalParams.PAD_SIZE * 2);
				
				container.addChild(pad);
				
				i++;
			}
			
			eaze(container).to(.2, { alpha:1 } )
						
			addEventListener(SaveManagerEvent.REFRESH_PADSMANAGER, refreshManager);
		}
		
		/* KeyBoard */
		public function manageKeyDown(e:KeyboardEvent):void 
		{
			var key : int = e.keyCode;
			
			if ( key == Keyboard.SPACE) {
					dispatchEvent(	new SoundSystemEvent(SoundSystemEvent.KILL_SOUND, null, true )	);
			}else if (key == Keyboard.CONTROL) {
				dispatchEvent(new Event(Controls.OPEN_CLOSE, true));
			}else {
				
				// secure
				if ( HardcoreGhettoSoundSystem.a_sndChannel.length > HardcoreGhettoSoundSystem.LIMIT) return;
				
				for each( var pad : Pad in vecPads )
				{
					if ( pad.key == key ) 
					{
						pad.registerSound();
						break;
					}
				}
			}
		}
		
		
		/**
		 * Création d'un nouveau pad 
		 * @param	e
		 */
		private function refreshManager(e:SaveManagerEvent):void 
		{
			var freePad : Pad = new Pad();
			freePad.keyId = int(e.data) + 1;
			vecPads.push(freePad);
			//var track:Track = new Track();
			//track.keyId = freePad.keyId;
			//vecTracks.push(track);
			
			/**			>>**--		R E S E T   D A T A S		--**<<
			 */
			Conf.deleteProperty("tracks");
			Conf.deleteProperty("pads");
			
			var conf : String = "xml/config.xml";
			
			Conf.addEventListener(Event.COMPLETE, displayGrid);
			Conf.loadXml( new URLRequest ( conf ));
		}
		
		public function dispose() : void
		{
			removeEventListener(SaveManagerEvent.REFRESH_PADSMANAGER, refreshManager);
			Conf.removeEventListener(Event.COMPLETE, displayGrid);
		}
		
	}
	
}